﻿@{
    if (false)
    {
        <script src="~/LiberUI/Scripts/jquery-2.0.3.js"></script>
        <script src="~/LiberUI/Scripts/jquery.ba-resize.js"></script>
        <script src="~/LiberUI/Scripts/LiberUI.Base.js"></script>
        <script src="~/LiberUI/Scripts/LiberUI.Controllers.js"></script>
        <script src="~/LiberUI/Scripts/LiberUI.Action.js"></script>
        <script src="~/SiteUI/BaseScripts/site.js"></script>
    }
}

<div id="frmNewProject">
    <input id="txbProjName" />
    <div id="slcBelongProj" class="Selector"></div>
</div>

<script>
    var wgProject = {};

    wgProject.onSubmitClick=function() {
        if (wgProject.Checker.Check()) {
            wgProject.frmProjectEdit._fireEvent("submit");
        }
    };
    wgProject.onShowWnd=function() {
        wgProject.Checker.Enable();
        wgProject.frmProjectEdit.GetItem(0).focus().select();
        wgProject.mnuProjects.Viewer(wgProject.frmProjectEdit.GetItem(1), wgProject.SetParentProj);
    };
    wgProject.onHideWnd=function() {
        wgProject.Checker.Reset();
        wgProject.mnuProjects.hide();
    };
    wgProject.SetParentProj = function (_viewer, _item) {
        _viewer.val(_item.Data);
    };

    function ProjViewer(_srcElm,_opt) {
        Controller.call(this, _srcElm, _opt);
        var instance = this;
        instance._member.$rootElm.addClass('ProjViewer');

        instance.af_setValue = function (_newValue) {
            instance._member.$rootElm.text(_newValue.Name);
        };

        instance._member.vf_reset = function () {
            instance._member.$rootElm.text('');
        };

        this.constructor = ProjViewer;
        return this;
    };
    ProjViewer.prototype = Controller.prototype;

    $(function () {
        wgProject.frmProjectEdit = new Form($("#frmNewProject"));
        wgProject.frmProjectEdit.AddItem("项目名称", $("#txbProjName"), '', true)
                                .AddItem("父级项目",new ProjViewer($("#slcBelongProj")),'')
                                .AddItem("项目描述", new HtmlEditor(), 'max');

        wgProject.frmProjectEdit.af_getValue = function (v) {
            v.Name = wgProject.frmProjectEdit.GetItem(0).val();
            v.Describe = wgProject.frmProjectEdit.GetItem(2).val();
            v.PIdStr = wgProject.frmProjectEdit.GetItem(1).val().IDStr;
            return v;
        };
        wgProject.frmProjectEdit.af_setValue = function (_proj) {
            wgProject.frmProjectEdit.GetItem(0).val(_proj.Name);

            //根据父id找到对应的列表项
            var p = wgProject.mnuProjects.Menu().Find(function (i) { return i.Data.IDStr == _proj.PIdStr; });
            if (p !== null) {
                wgProject.frmProjectEdit.GetItem(1).val(p.Data);
            }
            else {
                wgProject.frmProjectEdit.GetItem(1).reset();
            }
            wgProject.frmProjectEdit.GetItem(2).val(unescape(_proj.Describe));
        };

        wgProject.WndProjectEdit = new Dialoger(false).on('show', wgProject.onShowWnd).on('hide', wgProject.onHideWnd);
        wgProject.WndProjectEdit.BtnEnter('提交', wgProject.onSubmitClick);
        wgProject.WndProjectEdit.Controller(wgProject.frmProjectEdit.dom());

        wgProject.Checker = new Validater({ BlurOnError: true, CheckOnBlur: false })
                                        .NoEmpty(wgProject.frmProjectEdit.GetItem(0), "", "请填写项目名称");

        wgProject.mnuProjects = new PopMenu();
        var lstProjs = new List(null,{ ShowHead: false, ShowFoot: false, Selectble: false,Scroller:false });
        lstProjs.af_makeView = function (_data) {
            var li = $("<div class='projItem'>").text(_data.Name);
            return li;
        };
        wgProject.mnuProjects.Menu(lstProjs,'itemClick');
    })

</script>
